package com.ccc.boot.system.controller;

import com.ccc.boot.base.BaseController;
import com.ccc.boot.constant.AddrConstant;
import com.ccc.boot.constant.CommonConstant;
import com.ccc.boot.easypoi.util.MyExcelUtils;
import com.ccc.boot.pojo.AddrTree;
import com.ccc.boot.response.ResponseResult;
import com.ccc.boot.system.entity.SysAddr;
import com.ccc.boot.system.service.SysAddrService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

/**
 * 行政分区基本信息表 REST接口
 *
 * @author superC
 * @version 0.1.0
 * @date 2022-04-14
 */

@RestController
@RequestMapping(CommonConstant.SYS_URI_PREFIX + "/addr")
public class SysAddrController extends BaseController<SysAddr, SysAddrService> {

    /**
     * 根据所传行政分区，获取本机及下级tree
     *
     * @author superC
     * @date 2020/3/15 12:17
     */
    @GetMapping("/getAddrTree")
    public ResponseResult getAddrTree() {
        String adCd = AddrConstant.COUNTRY_CD;
        List<AddrTree> tree = service.getAddrTree(adCd);
        return ResponseResult.success("获取菜单数据成功", tree);
    }

    /**
     * 懒加载 获取一级行政分区树
     *
     * @author superC
     * @date 2020/3/15 12:17
     */
    @GetMapping("/loadFirstAddrTree")
    public ResponseResult loadFirstAddrTree() {
        List<AddrTree> tree = service.loadFirstAddrTree();
        return ResponseResult.success("获取一级行政分区树成功", tree);
    }

    /**
     * 懒加载 获取子集行政分区树
     *
     * @param id:
     * @author superC
     * @date 2020/3/15 12:17
     */
    @GetMapping("/getSonAddrTree/{id}")
    public ResponseResult getSonAddrTree(@PathVariable Long id) {
        List<AddrTree> tree = service.getSonAddrTree(id);
        return ResponseResult.success("获取子集行政分区树成功", tree);
    }

    /**
     * 导出Excel
     *
     * @param list:
     * @author superC
     * @date 2020/12/21 18:04
     */
    @PostMapping(value = "/exportExcel")
    public void exportExcel(@RequestBody List<SysAddr> list) {
        try {
            exportAddr(list);
        } catch (Exception e) {
            logger.error("【捕获异常-导出Excel】\r\n异常记录：", e);
        }
    }

    /**
     * 导出Excel-全部数据
     *
     * @author superC
     * @date 2020/12/21 18:04
     */
    @GetMapping(value = "/exportExcel2AllData")
    public void exportExcel2AllData() {
        try {
            exportAddr(service.getAllList());
        } catch (Exception e) {
            logger.error("【捕获异常-导出Excel】\r\n异常记录：", e);
        }
    }

    private void exportAddr(List<SysAddr> list) throws IOException {
        MyExcelUtils.exportExcel(list, "行政分区信息", "行政分区", SysAddr.class, "行政分区", response);
    }

}
